package com.igoal.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.igoal.dao.MessageDAO;
import com.igoal.dao.UserDAOExt;
import com.igoal.dto.MessageDTO;
import com.igoal.entity.Message;
import com.igoal.entity.User;
import com.igoal.entity.factory.EntityFactory;

@Service("messageManager")
public class MessageManagerImpl extends BaseService<MessageDTO, Message, Long>
		implements MessageManager {

	private MessageDAO messageDAO;

	@Qualifier("userDAOExt")
	private UserDAOExt userDAO;

	public MessageManagerImpl() {
		logger = LogFactory.getLog(this.getClass());
	}

	@Autowired
	public void setMessageDAO(MessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}

	@Autowired
	public void setUserDAO(UserDAOExt userDAO) {
		this.userDAO = userDAO;
	}

	public List<Message> getAll() {
		return messageDAO.getAll();
	}

	public Message get(Long id) {
		return messageDAO.get(id);
	}

	public boolean exists(Long id) {
		return messageDAO.exists(id);
	}

	public Message save(Message object) {
		return messageDAO.save(object);
	}

	public void remove(Long id) {
		messageDAO.remove(id);
	}

	public List<MessageDTO> getNewMessage(Long userId) {
		logger.info("Get new messages for the user with id = " + userId);
		List<MessageDTO> result = new ArrayList<MessageDTO>();
		try {
			List<Message> messages = messageDAO.getMessage(userId, false);
			if (messages != null) {
				for (Message msg : messages) {
					MessageDTO dto = new MessageDTO();
					dto.initFrom(msg);
					result.add(dto);
				}
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return result;
	}

	public List<MessageDTO> getReceivedMessage(Long userId) {
		logger.info("Get messages for the user with id = " + userId);
		List<MessageDTO> result = new ArrayList<MessageDTO>();
		try {
			List<Message> messages = messageDAO.getReceivedMessage(userId);
			if (messages != null) {
				for (Message msg : messages) {
					MessageDTO dto = new MessageDTO();
					dto.initFrom(msg);
					result.add(dto);
				}
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return result;
	}

	public List<MessageDTO> getSentMessage(Long userId) {
		logger.info("Get messages sent be the user with id = " + userId);
		List<MessageDTO> result = new ArrayList<MessageDTO>();
		try {
			List<Message> messages = messageDAO.getSentMessage(userId);
			if (messages != null) {
				for (Message msg : messages) {
					MessageDTO dto = new MessageDTO();
					dto.initFrom(msg);
					result.add(dto);
				}
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return result;
	}

	public int saveMessage(MessageDTO dto) {
		Long to = dto.getToId();
		Long from = dto.getFromId();

		try {
			logger.info("Send message from user " + from + " to user " + to);
			User userTo = userDAO.get(to);
			User userFrom = userDAO.get(from);
			Message msg = EntityFactory.createMessage(userFrom, userTo);
			msg.setSubject(dto.getSubject());
			msg.setBody(dto.getBody());
			msg = messageDAO.save(msg);
			return (msg != null) ? SaveResult.SUCCESS : SaveResult.FAIL;
		} catch (Exception e) {
			logger.error(e);
			return SaveResult.FAIL;
		}
	}

}
